#import "JSONSerializer.h"
#import "CJSONDeserializer.h"
#import "CJSONSerializer.h"
#import "User.h"
#import "LoginResult.h"
#import "RegisterResult.h"
#import "DateUtils.h"
#import "ForgetPasswordResult.h"
#import "ActivateResult.h"
#import "ResendActivateCodeResult.h"

#define JSON_FIELD_USER_NAME "username"
#define JSON_FIELD_PASSWORD "password"
#define JSON_FIELD_SUCCESS "success"
#define JSON_FIELD_USER_ID "user_id"
#define JSON_FIELD_API_KEY "api_key"
#define JSON_FIELD_ERROR "error"
#define JSON_FIELD_ERROR_INACTIVE "inactive user"
#define JSON_FIELD_ERROR_INVALID "invalid user"
#define JSON_FIELD_ERROR_CODE "err_code"
#define JSON_FIELD_ERROR_MSG "err_msg"
#define JSON_FIELD_DATE_JOINED "date_joined"
#define JSON_FIELD_FIRST_NAME "first_name"
#define JSON_FIELD_LAST_LOGIN "last_login"
#define JSON_FIELD_LAST_NAME "last_name"
#define JSON_FIELD_PROFILE "profile"
#define JSON_FIELD_BIRTH_DATE "birth_date"
#define JSON_FIELD_CITY "city"
#define JSON_FIELD_EDUCATION "education_degree"
#define JSON_FIELD_GENDER "gender"
#define JSON_FIELD_HEIGHT "height"
#define JSON_FIELD_ID "id"
#define JSON_FIELD_LOCATION "location"
#define JSON_FIELD_MARITAL_STATUS "marital_status"
#define JSON_FIELD_MUG_SHOT "mugshot"
#define JSON_FIELD_OCCUPATION "occupation"
#define JSON_FIELD_PIN "pin"
#define JSON_FIELD_POSTCODE "postcode"
#define JSON_FIELD_PRIVACY "privacy"
#define JSON_FIELD_RESOURCE_URI "resource_uri"
#define JSON_FIELD_TELEPHONE "telephone"
#define JSON_FIELD_WEIGHT "weight"

@implementation JSONSerializer

+(NSData*)serializeUserForLogin:(User *)user
{
    NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:user.name, @JSON_FIELD_USER_NAME, user.password, @JSON_FIELD_PASSWORD, nil];
    NSString* resultStr = [[CJSONSerializer serializer] serializeDictionary:dictionary];
    
    return [resultStr dataUsingEncoding:NSUTF8StringEncoding];
}

+(LoginResult *)deserializeLoginResult:(NSString*)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    
    LoginResult* result = [[LoginResult alloc] init];
    result.isOnline = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_SUCCESS] boolValue];
    result.error = (NSString*)[objectDic objectForKey:@JSON_FIELD_ERROR];
    if([result.error isEqualToString:@JSON_FIELD_ERROR_INACTIVE])
    {
        result.userId = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_USER_ID] intValue];
        result.isInvalid = false;
        result.isActivated = false;
    }
    else if([result.error isEqualToString:@JSON_FIELD_ERROR_INVALID])
    {
        result.isInvalid = true;
        result.isActivated = false;
    }
    
    if(result.isOnline)
    {
        result.apiKey = (NSString*)[objectDic objectForKey:@JSON_FIELD_API_KEY];
        result.userId = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_USER_ID] intValue];
        result.isInvalid = false;
        result.isActivated = true;
    }
    
    return [result autorelease];
}

+(RegisterResult *)deserializeRegisterResult:(NSString *)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    
    RegisterResult* result = [[RegisterResult alloc] init];
    result.errorCode = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_ERROR_CODE] intValue];
    result.error = (NSString*)[objectDic objectForKey:@JSON_FIELD_ERROR_MSG];
    if(!result.errorCode)
    {
        result.apiKey = (NSString*)[objectDic objectForKey:@JSON_FIELD_API_KEY];
        result.userId = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_USER_ID] intValue];        
    }
    return [result autorelease];
}

+ (ForgetPasswordResult *)deserializeForgetPasswordResult:(NSString *)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    
    ForgetPasswordResult* result = [[ForgetPasswordResult alloc] init];
    result.errorCode = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_ERROR_CODE] intValue];
    result.error = (NSString*)[objectDic objectForKey:@JSON_FIELD_ERROR_MSG];
    if(!result.errorCode)
    {
        result.apiKey = (NSString*)[objectDic objectForKey:@JSON_FIELD_API_KEY];
        result.userId = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_USER_ID] intValue];        
    }
    
    return [result autorelease];    
}

+ (ActivateResult *)deserializeActivateResult:(NSString *)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    
    ActivateResult* result = [[ActivateResult alloc] init];
    result.errorCode = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_ERROR_CODE] intValue];
    result.error = (NSString*)[objectDic objectForKey:@JSON_FIELD_ERROR_MSG];
    
    return [result autorelease];       
}

+ (ResendActivateCodeResult *)deserializeResendActivateCodeResult:(NSString *)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];
    
    ResendActivateCodeResult* result = [[ResendActivateCodeResult alloc] init];
    result.errorCode = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_ERROR_CODE] intValue];
    result.error = (NSString*)[objectDic objectForKey:@JSON_FIELD_ERROR_MSG];
    
    return [result autorelease];        
}

+(User *)deserializeUser:(NSString *)json
{
    NSError* error = nil;
    NSDictionary* objectDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:[json dataUsingEncoding:NSUTF8StringEncoding] error:&error];    
    
    User* user = [[User alloc] init];
    user.userId = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_ID] intValue];
    user.name = (NSString*)[objectDic objectForKey:@JSON_FIELD_USER_NAME];
    user.firstName = (NSString*)[objectDic objectForKey:@JSON_FIELD_FIRST_NAME];
    user.lastName = (NSString*)[objectDic objectForKey:@JSON_FIELD_LAST_NAME];
    user.resourceUri = (NSString*)[objectDic objectForKey:@JSON_FIELD_RESOURCE_URI];
    user.city = (NSString*)[objectDic objectForKey:@JSON_FIELD_CITY];
    user.educationDegree = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_EDUCATION] intValue];
    user.gender = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_GENDER] intValue];
    user.height = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_HEIGHT] floatValue];
    user.weight = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_WEIGHT] floatValue];
    user.address = (NSString*)[objectDic objectForKey:@JSON_FIELD_LOCATION];
    user.maritalStatus = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_MARITAL_STATUS] intValue];
    user.occupation = [(NSNumber*)[objectDic objectForKey:@JSON_FIELD_OCCUPATION] intValue];
    user.pin = (NSString*)[objectDic objectForKey:@JSON_FIELD_PIN];
    user.postcode = (NSString*)[objectDic objectForKey:@JSON_FIELD_POSTCODE];
    user.privacy = (NSString*)[objectDic objectForKey:@JSON_FIELD_PRIVACY];
    user.phone = (NSString*)[objectDic objectForKey:@JSON_FIELD_TELEPHONE];
    
    NSDateFormatter* dateFormatter = [DateUtils getDetailDateFormatter];
    user.joinDate = [dateFormatter dateFromString:(NSString*)[objectDic objectForKey:@JSON_FIELD_DATE_JOINED]];
    user.lastSignin = [dateFormatter dateFromString:(NSString*)[objectDic objectForKey:@JSON_FIELD_LAST_LOGIN]];
    
    NSDateFormatter* dateFormatter2 = [DateUtils getDateFormatter:@DATE_FORMAT_YYYY_MM_DD];
    user.birthday = [dateFormatter2 dateFromString:(NSString*)[objectDic objectForKey:@JSON_FIELD_BIRTH_DATE]];
    
    
    return [user autorelease];
}

+(NSString *)serializeFeedback:(NSString *)content version:(NSString *)version agent:(NSString *)agent userUri:(NSString *)uri
{
    CJSONSerializer* serializer = [CJSONSerializer serializer];
    NSMutableDictionary* dic = [[[NSMutableDictionary alloc] init] autorelease];
    [dic setObject:version forKey:@"app_version"];
    [dic setObject:agent forKey:@"user_agent"];
    [dic setObject:content forKey:@"feedback_content"];
    [dic setObject:uri forKey:@"user"];
    return [serializer serializeDictionary:dic];
}

+(NSString *)serializeRecord:(Record *)record userUri:(NSString *)uri
{
    CJSONSerializer* serializer = [CJSONSerializer serializer];
    NSMutableDictionary* dic = [[[NSMutableDictionary alloc] init] autorelease];
    [dic setObject:version forKey:@"measure_time"];
    [dic setObject:agent forKey:@"user_agent"];
    [dic setObject:content forKey:@"feedback_content"];
    [dic setObject:uri forKey:@"user"];
    return [serializer serializeDictionary:dic];
}

@end
